<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="resource/css/bootstrap.css">
    <script type="text/javascript" src="resource/js/jquery-3.2.1.js"></script>
</head>
<body>
    维修单
    <form:form modelAttribute="weiXiu" action="add" method="post" enctype="multipart/form-data">
        <table>
            <tr>
                <td>联络单号</td>
                <td>
                    <form:input path="danHao"></form:input>
                </td>
            </tr>
            <tr>
                <td>报送日期</td>
                <td>
                    <form:input path="days" type="date"></form:input>
                </td>
            </tr>
            <tr>
                <td>制造部</td>
                <td>
                    <form:input path="zhiZao"></form:input>
                </td>
            </tr>
            <tr>
                <td>制造部编号</td>
                <td>
                    <form:input path="bianHao"></form:input>
                </td>
            </tr>
            <tr>
                <td>配件</td>
                <td>
                    <form:select path="peiJianId">
                        <form:option value="">===请选择===</form:option>
                    </form:select>
                </td>
            </tr>
            <tr>
                <td>金额</td>
                <td>
                    <form:input path="money" onblur="a()"></form:input>
                    <span id="sp1"></span>
                </td>
            </tr>
            <tr>
                <td>加工检验图片上传</td>
                <td>
                    <input type="file" name="file" onchange="upFile()">
                    <form:input path="imgs" readonly="readonly" onblur="b()"></form:input>
                    <span id="sp2"></span>
                </td>
            </tr>
            <tr>
                <td>采购人</td>
                <td>
                    <form:input path="cPerson"></form:input>
                </td>
            </tr>
            <tr>
                <td>报销人</td>
                <td>
                    <form:input path="bPerson"></form:input>
                </td>
            </tr>
            <tr>
                <td>备注</td>
                <td>
                    <form:textarea path="beiZhu"></form:textarea>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="button" value="提交" onclick="c()">
                </td>
            </tr>
        </table>
    </form:form>

    <script type="text/javascript">
        //配件下拉框
        $.post(
            "peiJianAll",
            function (msg){
                var str = "<option value=''>===请选择===</option>";
                for (var i = 0; i < msg.length; i++) {
                    str += "<option value='"+msg[i].id+"'>"+msg[i].id+"-"+msg[i].name+"</option>";
                }
                $("[name='peiJianId']").html(str);
            }
        )
        //上传图片
        function upFile(){
            var  formDate = new FormData($("form")[0]);
            $.ajax({
                url:"upFile",
                data:formDate,
                type:"post",
                contentType:false,
                processData:false,
                success:function (msg){
                    $("[name='imgs']").val(msg);
                }
            })
        }
        //校验

        function c(){
            //验证金额
            var flag = true;
            var money = $("[name='money']").val();
            var regMoney = /^\d+\.\d{1,2}$/;
            if (money != 0){
                if (regMoney.test(money)){
                    flag = true;
                    $("#sp1").html("")
                }else{
                    flag = false;
                    $("#sp1").html('<font style="color: red">金额可以是整数、1位小数、2位小数</font>')
                }
            }else{
                $("#sp1").html('<font style="color: red">金额不能为0</font>')
            }

            //验证图片
            var imgs = $("[name='imgs']").val();
            var regImgs = /\w+(.bmp)|(.jpg)|(.png)|(.tif)|(.gif)/;
            if (regImgs.test(imgs)){
                flag = true;
            }else{
                flag = false;
                $("#sp2").html('<font style="color: red">校验不是常用图片类型[bmp,jpg,png,tif,gif等]不能上传</font>')
            }
            //提交
            if (flag){
                $("form").submit();
            }
        }

    </script>
</body>
</html>
